package com.lmc.health.rate;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lmc.R;
import com.lmc.entity.Health;
import com.lmc.entity.HeartRate;
import com.lmc.entity.HeartType;
import com.lmc.util.JsonManager;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.FamilyMemberBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.util.SharePreferenceUtil;

import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 健康数据详情
 */
public class HeartRateActivity extends BaseActivity {

    private TextView analyze;
    private TextView data;
    private RelativeLayout fragment_layout;
    private AnalyzeFragment analyzeFragment;
    private DataFragment dataFragment;
    public Health.DataBean.ResultBean health;

    public static void start(Context context, Health.DataBean.ResultBean health) {
        Intent intent = new Intent(context, HeartRateActivity.class);
        intent.putExtra("health", health);
        context.startActivity(intent);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_heart_rate;
    }

    @Override
    protected void initView() {

        analyze = (TextView) findViewById(R.id.analyze);
        analyze.setOnClickListener(this);
        data = (TextView) findViewById(R.id.data);
        data.setOnClickListener(this);
        fragment_layout = (RelativeLayout) findViewById(R.id.fragment_layout);
        fragment_layout.setOnClickListener(this);
        health = (Health.DataBean.ResultBean) getIntent().getSerializableExtra("health");
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText(health.getName());
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        analyzeFragment = new AnalyzeFragment();
        dataFragment = new DataFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_layout, analyzeFragment)
                .add(R.id.fragment_layout, dataFragment)
                .hide(dataFragment)
                .show(analyzeFragment)
                .commitAllowingStateLoss();


    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        Drawable drawable = getResources().getDrawable(R.drawable.h_t_b_15);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        if (view.getId() == R.id.analyze) {
            analyze.setCompoundDrawables(null, null, null, drawable);
            data.setCompoundDrawables(null, null, null, null);
            analyze.setTextColor(Color.parseColor("#ff2a8cff"));
            data.setTextColor(Color.parseColor("#FF333333"));
            updateFragment(0);

        } else if (view.getId() == R.id.data) {
            data.setCompoundDrawables(null, null, null, drawable);
            analyze.setCompoundDrawables(null, null, null, null);
            data.setTextColor(Color.parseColor("#ff2a8cff"));
            analyze.setTextColor(Color.parseColor("#FF333333"));
            updateFragment(1);
            analyzeFragment.hideDataDialog();
        }
    }


    private void updateFragment(int i) {
        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
        trx.hide(analyzeFragment).hide(dataFragment);
        if (i == 0) {
            trx.show(analyzeFragment).commitAllowingStateLoss();
        } else {
            trx.show(dataFragment).commitAllowingStateLoss();
        }
    }

}
